/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dalobject;

import dalInterface.DALRelation;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.DataTable;

/**
 *
 * @author Mrkupi
 */
public class DALRelationImpl implements DALRelation
{
    private final Connection sqlConnection;
    
    public DALRelationImpl( Connection conn )
    {
        this.sqlConnection  =   conn;
    }
    
    
    @Override
    public boolean requestFriend(String userName, String friendName) throws RemoteException 
    {
        synchronized (sqlConnection)
        {
            try 
            {
                Statement st = sqlConnection.createStatement();
                String queryString  =   String.format("insert into REQUEST values (\'%s\', \'%s\', \'\')", userName, friendName);
                return st.execute( queryString );
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return false;
        }
    }

    @Override
    public DataTable getRequestedFriend( String userName ) throws RemoteException 
    {
        synchronized (sqlConnection)
        {
            try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("select * from REQUEST WHERE REQUESTTO=\'%s\'", userName);

                return DataTable.createDataTable(st.executeQuery(queryString), "RequestFrom");
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return null;
        }
    }

    @Override
    public boolean insertNewRelationShip( String userName, String friend ) throws RemoteException 
    {
        synchronized (sqlConnection)
        {
            try 
            {
                Statement st = sqlConnection.createStatement();
                String queryString  =   String.format("insert into RELATION values (\'%s\', \'%s\', %d)", userName, friend, 1);
                return st.execute( queryString );
            }
            catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return false;
        }
    }

    @Override
    public boolean deleteRequest(String userName, String requester) throws RemoteException 
    {
        synchronized (sqlConnection)
        {
            try 
            {
                Statement st = sqlConnection.createStatement();
                String queryString  =   String.format( "delete from REQUEST where REQUESTFROM=\'%s\' AND REQUESTTO=\'%s\'", requester, userName);
                return st.execute( queryString );

            }
            catch (SQLException ex)
            {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return false;
        }
    }

    @Override
    public DataTable getFriendOf(String accountName) throws RemoteException 
    {
        synchronized (sqlConnection)
        {
         try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("select * from RELATION WHERE USERNAME1=\'%s\' or USERNAME2=\'%s\'", accountName, accountName);

                return DataTable.createDataTable(st.executeQuery(queryString), "USERNAME1", "USERNAME2");
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return null;
        }
    }
}
